home *** CD-ROM | disk | FTP | other *** search
/ BMUG Revelations / BMUG Revelations.toast / Utilities / Text and Speech / UUTool / uu engine / UUGlue.h < prev    next >
Text File  |  1991-06-12  |  7KB  |  265 lines

  1. /*
  2. **    Project:    uu**code engine
  3. **    Author:        Bernhard S. Wieser
  4. **    Module:        uuglue
  5. **    Date:        5/19/91
  6. **    Revised:    6/1/91        L.R. suggested user allocate buffers
  7. **                6/8/91        L.R. suggested Pascal calling conventions
  8. **                            - removed table/engine globals
  9. **    Version:    1.0.2
  10. **
  11. **    Preface:
  12. **        uu**code engine is © by Bernhard S. Wieser and Octavian Micro
  13. **    development, all rights reserved.  Please read the accompanying
  14. **    licensing agreement before use.
  15. **
  16. **    Purpose:
  17. **        This code should be compiled with the program using the
  18. **    uu**coding engine.  It loads and unloads the resource, as well as
  19. **    providing 'glue' to the contained routines.
  20. **        The 'glue' routines bind the various uu**coding functions
  21. **    contained in the resource file to known subroutines names, thus
  22. **    providing a means of easy access from a high level language.
  23. **    The underlying resource routines use the Pascal calling convention.
  24. **        The resources occupy <3K of memory.  You must make room for
  25. **    the buffers, except for the uuencode() and uudecode() routines which
  26. **    require that you specify how much (contiguous) space they will allocate.
  27. **        To be safe (especially with noxious INITs which invade an
  28. **    applications memory), allocate at least (volume buffer size)
  29. **    524 bytes + some so as not to unexpectedly run out of memory.
  30. **
  31. **    Conditions of use:
  32. **        Visible credit to Bernhard S. Wieser and Octavian Micro
  33. **    Development MUST be given.  Written permission MUST be obtained
  34. **    upon any distribution, with a licensing fee pending depending on what
  35. **    it is used for.
  36. **
  37. **    Algorithm:
  38. **        uuencoding takes 24 bit from input stream (3 bytes) and splits
  39. **    it into 32 bits (4 bytes) output.  Each output byte has the top 2
  40. **    bits clear, and can be translated into a printable character.  The
  41. **    traditional way is to simply add $20 (space).  uudecoding simply
  42. **    subtracts $20, strips the top 2 bits from input, and stuffs things 
  43. **    back together.
  44. **        The encoding engine uses a translate table. Two reasons.
  45. **    First, its faster than bit manipulation.  Second, I can avoid
  46. **    some characters (like space, which some mailers despise).  This
  47. **    table can be changed so long as the character, when its top 2
  48. **    bits are stripped, equals its index into the table.
  49. **
  50. **        Example:
  51. **                  '`' = $60,    ' ' = $20    <- encoded
  52. **                  AND    $3F              $3F
  53. **                          ___              ___
  54. **                        $20              $20
  55. **                  SUB    $20              $20
  56. **                          ___              ___
  57. **                        $00              $00    <- decoded
  58. **
  59. **    Data Structures:
  60. **        Here is the resource 'UENG' 128 jump table:
  61. **                OFFSET    CODE
  62. **                   0    BRA    encode
  63. **                  +4    BRA    encodeOne
  64. **                  +8    BRA    encodeLine
  65. **                  +C    BRA    uuencode
  66. **                 +10    BRA    decode
  67. **                 +14    BRA    decodeOne
  68. **                 +18    BRA    decodeLine
  69. **                 +1C    BRA    uudecode
  70. **
  71. **        Resource 'HEXA' 128 is an array of 64 printable characters
  72. **        which equal their index when the top two bits are stripped.
  73. **
  74. **    Revisions
  75. **        User must now maintain the table and engine handles.  User
  76. **    Must also remember these are LOCKED so must be dereferenced and
  77. **    passed as pointers to the core routines.
  78. */
  79.  
  80. /*
  81. **    Load and Unload of uu**engine
  82. */
  83. pascal OSErr UULoad(
  84.     Handle *,    /* pointer to the handle of returned look-up table */
  85.     Handle *    /* pointer to the handle of returned engine base address */
  86.     );
  87. pascal void UUnload(
  88.     Handle,        /* handle to the look-up table */
  89.     Handle        /* handle to the engine base address */
  90.     );
  91.  
  92. /*                                    **
  93. **    The core Pascal glue routines!    **
  94. **                                    */
  95.  
  96. /*
  97. **    encodeOne
  98. **        Encode one byte only, only use low 6 bits input.
  99. **
  100. **    Parameters
  101. **        c        the character (passed as integer)
  102. **        table    pointer to the encode table
  103. **        engine    pointer to the engine base
  104. **
  105. **    Returns
  106. **        the encoded character (as integer)
  107. */
  108. pascal short encodeOne(
  109.     short,
  110.     Ptr,
  111.     Ptr
  112.     ) = { 0x205F, 0x4EA8, 0x0004 };
  113.     
  114. /*
  115. **    encode
  116. **        Take three bytes input, return four bytes uucoded output.
  117. **
  118. **    Parameters
  119. **        three    pointer to an array holding at least three characters
  120. **        table    pointer to the encode table
  121. **        engine    pointer to the engine base
  122. **
  123. **    Returns
  124. **        four characters of encoded data in a long word
  125. */
  126. pascal OSType encode(
  127.     void *,
  128.     Ptr,
  129.     Ptr
  130.     ) = { 0x205F, 0x4E90 };
  131.  
  132. /*
  133. **    encodeLine
  134. **        Take 'insize' bytes from buffer 'in' and encode to buffer 'out'.
  135. **    The size of the encode data is returned (including length and CR).
  136. **    Remember, input buffer should be divisible by 3, and output size
  137. **    will be 4 times the quotient (plus 2 for length and CR).
  138. **
  139. **    Parameters
  140. **        in        pointer to input buffer
  141. **        insize    size of input buffer
  142. **        out        pointer to output buffer
  143. **        table    pointer to the encode table
  144. **        engine    pointer to the engine base
  145. **
  146. **    Returns
  147. **        length of encoded data in output buffer
  148. */
  149. pascal long encodeLine(
  150.     void *,
  151.     int,
  152.     void *,
  153.     Ptr,
  154.     Ptr
  155.     ) = { 0x205F, 0x4EA8, 0x0008 };
  156.  
  157. /*
  158. **    uuencode
  159. **        Encode file 'infile' to output file 'outfile', calling
  160. **    'routine' as buffers are flushed.  'routine' should be NULL
  161. **    if there are no periodic tasks to perform.  'numbufrs' tells the
  162. **    engine how many input/output buffers to allocate.  An input
  163. **    buffer is 45 bytes long, and output buffer is 62 bytes long.
  164. **    Example, numbufrs = 1024 would allocate 45K+62K = 107K.
  165. **
  166. **    Parameters
  167. **        infile        input file's file reference number
  168. **        outfile        output file's "        "        "
  169. **        numbufrs    number of i/o buffers to use (not size of!)
  170. **        routine        update procedure pointer (or NIL)
  171. **        table        pointer to the encode table
  172. **        engine        pointer to the engine base
  173. **
  174. **    Returns
  175. **        any error encountered
  176. */
  177. pascal OSErr uuencode(
  178.     int,
  179.     int,
  180.     int,
  181.     void *,
  182.     Ptr,
  183.     Ptr
  184.     ) = { 0x205F, 0x4EA8, 0x000C };
  185.  
  186. /*
  187. **    decodeOne
  188. **        Decode one coded character.
  189. **
  190. **    Parameters
  191. **        c        encoded character (integer passed)
  192. **        engine    pointer to the engine's base address
  193. **
  194. **    Returns
  195. **        the decoded character (integer)
  196. */
  197. pascal short decodeOne(
  198.     short,
  199.     Ptr
  200.     ) = { 0x205F, 0x4EA8, 0x0014 };
  201.  
  202. /*
  203. **    decode
  204. **        Decode 4 bytes to 3.
  205. **
  206. **    Parameters
  207. **        s    pointer to 4 byte array of data to decode
  208. **        engine    pointer to the engine's base address
  209. **
  210. **    Returns
  211. **        the decoded data (in the upper 8 bits of a long word)
  212. */
  213. pascal OSType decode(
  214.     void *,
  215.     Ptr
  216.     ) = { 0x205F, 0x4EA8, 0x0010 };
  217.  
  218. /*
  219. **    decodeLine
  220. **        Decode a uucoded input in 'in' to buffer 'out'.  Returns
  221. **    length of decoded data.
  222. **
  223. **    Parameters
  224. **        in        pointer to an input buffer, of which first encoded
  225. **                character is buffer length
  226. **        out        pointer to the buffer to contained decoded data,
  227. **                generally < 80 bytes is required.
  228. **        engine    pointer to the engine's base address
  229. **
  230. **    Returns
  231. **        the amount of decoded data (or buffer size if you like)
  232. */
  233. pascal long decodeLine(
  234.     void *,
  235.     void *,
  236.     Ptr
  237.     ) = { 0x205F, 0x4EA8, 0x0018 };
  238.     
  239. /*
  240. **    uudecode
  241. **        Decode 'infile' to 'outfile', calling 'routine' on buffer flush.
  242. **    'bufsizes', unlike in uuencode, is the actual buffer size in bytes
  243. **    that you want to allocated for input and output buffers.
  244. **  NEVER allocate a buffer less than 10 bytes (it would be very silly).
  245. **    Suggested values are multiples of 524 bytes.
  246. **    Function returns error.
  247. **
  248. **    Parameters
  249. **        infile        file reference number of input (encoded) data
  250. **        outfile        "        "        "      of output (decoded) data
  251. **        bufsizes    the size in bytes for input and output buffers
  252. **                    to be allocated by uudecode()
  253. **        routine        the update procedure (or NIL if none)
  254. **        engine        pointer to the engine's base address
  255. **
  256. **    Returns
  257. **        any errors encountered
  258. */
  259. pascal OSErr uudecode(
  260.     int,
  261.     int,
  262.     long,
  263.     void *,
  264.     Ptr
  265.     ) = { 0x205F, 0x4EA8, 0x001C };